package com.alstudio.ui.module.main.fragment;

import java.util.ArrayList;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alstudio.module.xmpp.request.ALUiXmppRequestCreator;
import com.alstudio.pojo.ALXmppEvent;
import com.alstudio.pojo.module.user.User;
import com.alstudio.utils.log.ALLog;
import com.alstudio.view.absadapter.BaseUserListAdapter;
import com.alstudio.view.listview.PullRefreshAndLoadMoreView;
import com.alstudio.view.listview.PullRefreshAndLoadMoreView.RankViewLoadStateListener;
import com.loovee.imaohu.R;

public class FriendsFragment2 extends Fragment implements
		FragmentUpdateDataEngine {

	private PullRefreshAndLoadMoreView listview;

	private ArrayList<User> users = new ArrayList<User>();
	private BaseUserListAdapter adapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View v = inflater.inflate(R.layout.friends_fragment, container, false);
		initListView(v);
		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				invokeLoadData();
			}
		}, 1000);
		return v;
	}

	private void initListView(View parent) {
//		listview = (PullRefreshAndLoadMoreView) parent
//				.findViewById(R.id.listview);
		adapter = new BaseUserListAdapter(users);
		listview.setAdapter(adapter);

		listview.setLoadStateListener(rankViewLoadStateListener);
	}

	// 魅力榜滑动加载事件回调
	private RankViewLoadStateListener rankViewLoadStateListener = new RankViewLoadStateListener() {

		@Override
		public void onRefresh(PullRefreshAndLoadMoreView view) {
			// TODO Auto-generated method stub
			ALUiXmppRequestCreator.postALUiXmppLoadFriendsListEvent(0,
					view.startIndex + "", view.endIndex + "");

		}

		@Override
		public void onLoadMore(PullRefreshAndLoadMoreView view) {
			// TODO Auto-generated method stub
			ALUiXmppRequestCreator.postALUiXmppLoadFriendsListEvent(0,
					view.startIndex + "", view.endIndex + "");

		}
	};

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.ui.module.main.fragment.FragmentUpdateDataEngine#onDataUpdateReq
	 * (boolean, java.lang.Object, boolean)
	 */
	@Override
	public void onDataUpdateReq(boolean success, Object data, boolean hasMore) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.ui.module.main.fragment.FragmentUpdateDataEngine#onRefreshReq
	 * ()
	 */
	@Override
	public void onRefreshReq() {
		// TODO Auto-generated method stub
		invokeLoadData();
	}

	private void invokeLoadData() {
		listview.autoRefresh();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.ui.module.main.fragment.FragmentUpdateDataEngine#onDataUpdateReq
	 * (com.alstudio.pojo.ALXmppEvent)
	 */
	@Override
	public void onDataUpdateReq(ALXmppEvent data) {
		// TODO Auto-generated method stub

		// TODO Auto-generated method stub
		boolean success = false;
		boolean hasMore = data.getBoolean();
		if (data.getResponseCode() == ALXmppEvent.RESPONSE_OK) {
			// 数据获取成功
			ALLog.d("刷新好友数据成功");
			success = true;
		}

		if (success) {
			listview.updateLoadMoreState(hasMore);
			@SuppressWarnings("unchecked")
			ArrayList<User> tmps = (ArrayList<User>) data.getData();
			if (tmps.size() > 0) {
				if (listview.isRefreshing()) {
					users.clear();
				}
				users.addAll(tmps);

				tmps.clear();
				tmps = null;
			}
			adapter.notifyDataSetChanged();
		}
		listview.onLoadCompleted();

	}

}
